<?php

/**
 * Created by PhpStorm.
 * User: hda
 * Date: 2017/7/1
 * Time: 下午3:32
 */

namespace app\user\controller;

use icontroller\wechat\AController;
use library\common\drawing\ImageSynthetise;
use library\tencent\wechat\admin_wechat\WechatMpService;
use library\tencent\wechat\common\ApiClient;
use model\AccountRole;
use model\ShopCollect;
use model\ShopUserAddress;
use think\Cookie;
use think\Db;
use think\Request;
use model\Account as ac;

class Index extends AController
{
    public function index()
    {
        $ua = new ShopUserAddress();
        $info = ac::getField("money,frozen_money,headimg,nick,name,pay_password", ["id" => $this->account_id]);
        $address = $ua->where(["account_id" => $this->account_id, "flag_default" => 1])->value("address");
        $ar = AccountRole::get(["account_id" => $this->account_id, "role_id" => 2, "flag_delete" => 0]);
        if (!empty($ar)) {
            $info["is_delivery"] = 1;
        } else {
            $info["is_delivery"] = 0;
        }
        $this->assign("address", $address);
        $this->assign("info", $info);
        return view();
    }

    // <editor-fold defaultstate="collapsed" desc="获取收藏店铺">
    public function collect()
    {
        $request = Request::instance();
        if ($request->isAjax()) {
            $page = input('page', 0);                      //分页
            $count = config('paginate.list_rows');         //每页数量
            $city_searchValue = input("city_searchValue"); //城市查询

//          $sort = input('sort', 'desc');
//          $order = input('order', 'id');
            $keyword = input('keyword', '');
            $where = [
                "s.flag_lock" => 0,
                "c.account_id" => $this->account_id
            ];
            if ($keyword) {
                $where["s.name"] = ["like", "%$keyword%"];
            }
            if ($city_searchValue) {
                $where["s.city"] = $city_searchValue;
            }
            $mo = new ShopCollect();
            $list = $mo->alias("c")->where($where)->join("shop_store s", "c.store_id=s.id")->page($page, $count)->select();
            foreach ($list as &$v) {
                $v->logo = '/public/admin/static/upload/' . Db::name('file')->where(['id' => $v->logo])->value('filename');
            }
            $this->success("成功", null, $list);
        }
        return view();
    }

    // </editor-fold>

    public function down_image()
    {
        $media_id = input('media_id');
//        return down_image_form_wechat($media_id);
        $access_token = WechatMpService::getAccessToken();
        $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=$access_token&media_id=$media_id";
//        $data=ApiClient::download($url);
        $res = ApiClient::download($url);
        $data = $res->getData();
        $res = Db::name('account')->where(['id' => $this->aid])->setField('headimg', __STATIC__ . '/uploader/wx_uploader/' . $data["filename"]);
        if ($res) {
            $this->success("修改头像成功");

        } else {
            $this->error("修改头像失败");
        }
    }

    public static function save_headimg()
    {
        $url = "http://wx.qlogo.cn/mmopen/ibVZ9riaCjowlRibAoAA2HeeYFKUGrhYlbRHicDF0NB9yE6zakG65mvtUJyHibGRfcME0mxxuBlteqbNKsFxpS0XurYV2wkmiac6iaQ/0";
        $path = ImageSynthetise::create_path("./public/wechat/static/uploader/wx_uploader/", 'png');
        $ch = curl_init();
        $httpheader = array(
            'Host' => 'mmbiz.qpic.cn',
            'Connection' => 'keep-alive',
            'Pragma' => 'no-cache',
            'Cache-Control' => 'no-cache',
            'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8',
            'User-Agent' => 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36',
            'Accept-Encoding' => 'gzip, deflate, sdch',
            'Accept-Language' => 'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4'
        );
        $options = array(
            CURLOPT_HTTPHEADER => $httpheader,
            CURLOPT_URL => $url,
            CURLOPT_TIMEOUT => 5,
            CURLOPT_FOLLOWLOCATION => 1,
            CURLOPT_RETURNTRANSFER => true
        );
        curl_setopt_array($ch, $options);
        $result = curl_exec($ch);
        curl_close($ch);
        file_put_contents($path, $result);
        $CodePath = str_replace('./', '/', $path);
        return $CodePath;
    }

    // <editor-fold defaultstate="collapsed" desc="记录用户坐标">
//    public function save_point()
//    {
//        $lng = input("lng");
//        $lat = input("lat");
//        if (!empty($lng) && !empty($lat)) {
//            Cookie::set('point', ["lng" => $lng, "lat" => $lat], 3600);
//        }
//    }
    // </editor-fold>
}